package p83;

import node.ListNode;
import node.ListNodeManager;

/***83. 删除排序链表中的重复元素
 给定一个排序链表，删除所有重复的元素，使得每个元素只出现一次。

 示例 1:

 输入: 1->1->2
 输出: 1->2
 示例 2:

 输入: 1->1->2->3->3
 输出: 1->2->3
 通过次数161,892提交次数313,106***/
public class Solution {
    public static void main(String[] args) {
        int[] nums = {1, 1, 2, 3, 3};
        ListNode head = ListNodeManager.createListNodeByArray(nums);
        ListNodeManager.printlnNode(deleteDuplicates(head));
    }

//    public static ListNode deleteDuplicates(ListNode head) {
//        if (head == null || head.next == null)
//            return head;
//        head.next = deleteDuplicates(head.next);
//        if (head.val == head.next.val) {
//            return head.next;
//        }
//        return head;
//    }

    public static ListNode deleteDuplicates(ListNode root) {
        ListNode head = root;
        while (head != null && head.next != null) {
            if (head.val == head.next.val) {
                head.next = head.next.next;
            } else {
                head = head.next;
            }
        }
        return root;
    }
}
